const app = getApp();
const WXAPI = require('../../wxapi/domain')
const WxParse = require('../../wxParse/wxParse.js');
const regeneratorRuntime = require('../../utils/runtime')
const dateTimePicker = require('../../utils/dateTimePicker.js');
const chooseLocation = requirePlugin('chooseLocation');
const CONFIG = require('../../config.js')

Page({
  data: {
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2000,
    endYear: 2050,
    address:"",
    title:"",
    phone: "",
    actionDate: "",
    location: null,
  },
  onLoad() {
    const that = this
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    that.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      title: '',
      phone: '',
      context: '',
      actionDate: null,
      location: null,
    });
  },
  onShow(e) {
    const openid = wx.getStorageSync('openid');
    if (!openid) {
      app.goLoginPageTimeOut()
      return
    }
    wx.checkSession({
      fail() {
        app.goLoginPageTimeOut()
      }
    })
  },
  onChooseLocation() {
    wx.chooseLocation({
      success: (res) => {
        this.setData({
          location: res
        });
      }
    });
  },
  changeDate(e) {
    this.setData({ date: e.detail.value });
  },
  changeTime(e) {
    this.setData({ time: e.detail.value });
  },
  changeDateTime(e) {
    this.setData({ dateTime: e.detail.value });
  },
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },
  changeDateTimeColumn(e) {
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  commitAction(e) {
    var that = this
    const openid = wx.getStorageSync('openid');
    if (!openid) {
      app.navigateToLogin = false
      app.goLoginPageTimeOut()
      return
    }
    if (!that.data.title){
      wx.showToast({
        title: '活动主题不能为空',
        icon: 'none'
      })
      return
    }
    if (!that.data.location) {
      wx.showToast({
        title: '请选取活动地点',
        icon: 'none'
      })
      return
    }
    if (!that.data.phone) {
      wx.showToast({
        title: '联系电话不能为空',
        icon: 'none'
      })
      return
    }
    if (!that.data.actionDate) {
      wx.showToast({
        title: '请选择活动时间',
        icon: 'none'
      })
      return
    }
    if (!that.data.context) {
      wx.showToast({
        title: '活动详情不能为空',
        icon: 'none'
      })
      return
    }
    WXAPI.commitAction({
      address: that.data.location.address,
      latitude: that.data.location.latitude,
      longitude: that.data.location.longitude,
      addressName: that.data.location.name,
      title: that.data.title,
      context: that.data.context,
      actionDate: that.data.actionDate,
      openId: openid,
      phone: that.data.phone
    }).then(function (res) {
      
      if (res.code === 0) {
        wx.showToast({
          title: '提交成功',
        })
        //刷新当前页面的数据
        that.onLoad();
      } else {
        wx.showToast({
          title: '服务器发生异常',
          icon: 'none'
        })
      }
    })
  },
  getTitle: function (e) {
    var that = this
    var val = e.detail.value;
    that.setData({
      title: val
    });
  },
  getPhone: function (e) {
    var that = this
    var val = e.detail.value;
    that.setData({
      phone: val
    });
  },
  getContext: function (e) {
    var that = this
    var val = e.detail.value;
    that.setData({
      context: val
    });
  },
  getActionDate: function (e) {
    var that = this
    var val = e.detail.value
    that.setData({
      actionDate: val
    });
  }
})